package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.Food;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FoodListAdapter extends ArrayAdapter<Food> {
	private LayoutInflater mInflater;
	
	public FoodListAdapter(Context context, int resource, int textViewResourceId, 
			List<Food> objects, LayoutInflater inflater) {
		super(context, resource, textViewResourceId, objects);
		mInflater = inflater;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {   
		ViewHolder holder = null;
		TextView name = null;
		TextView likes = null;
		TextView description = null;
		RemoteImageView image = null;
		
		Food food = getItem(position);
		
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.food_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();
		
		image = holder.getImageView();
		image.setImageUrl(food.imageUrl);
		image.loadImage();
		
		name = holder.getNameView();
		name.setText(food.name);
		
		likes = holder.getLikeView();
		likes.setText(food.numEaten + " people meal this up");
		
		description = holder.getDescriptionView();
		//description.setText(food.description);

		return convertView;
	}
	
	private class ViewHolder {
		private View vRow;
		private TextView vName = null;
		private TextView vLike = null;
		private TextView vDescription = null;
		private RemoteImageView vImage = null;

		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getNameView() {
			if (vName == null) {
				vName = (TextView) vRow.findViewById(R.id.name);
			}
			return vName;
		}
		
		public TextView getLikeView() {
			if(vLike == null) {
				vLike = (TextView) vRow.findViewById(R.id.like);
			}
			return vLike;
		}
		
		public TextView getDescriptionView() {
			if (vDescription == null) {
				vDescription = (TextView) vRow.findViewById(R.id.desc);
			}
			return vDescription;
		}

		public RemoteImageView getImageView() {
			if (vImage == null) {
				vImage = (RemoteImageView) vRow.findViewById(R.id.img);
			}
			return vImage;
		}
	}
}
